package com.skyworth.skymanualActivity;
import java.util.ArrayList;
import java.util.HashMap;
import com.skyworth.skymanualdata.ContentData;
import com.skyworth.skymanualdata.DataAdapter;
import com.skyworth.skymanualdata.SkyworthKeyCode;
import com.skyworth.skymanual.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsoluteLayout;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class SecondActivity extends Activity {
	AbsoluteLayout myabslayout;
	LinearLayout firstlayout, secondlayout, thirdlayout;	
	private TextView egOnly,egCommon;
	private ImageView zeroimage;
	
	private ImageView enlargeimage;
	private ImageView thirdotherimage, secondotherimage;
	private ImageView thirdotherimageback, secondotherimageback;
	private Animation animationthirdotherright,animationthirdotherleft,animationsecondotherright,animationsecondotherleft;
	private Animation[] myAnimationRight = new Animation[5];
	private Animation[] myAnimationLeft = new Animation[4];
	private Animation zeroleft;
	private ArrayList<HashMap<String, Object>> list;
	DataAdapter mListViewAdapter;
	private ImageView[] mSencondView;
	private ImageView[] mSencondViewback;
	ArrayList<HashMap<String, Object>> listItem;
	public static int flag = 2;
 	public int picindex = 1;
	public int picnum = 2;
	private static int SecondViewPageNum = 0;
	TextView titletext;
	public String[] titlestr = { "", "", "", "", "" };
	public int[] picture = { 0, 0, 0, 0, 0 };
	public int[] mark = { 0, 0, 0, 0, 0 };
	public int[] mark_back = { 0, 0, 0, 0, 0 };
	public int[] common_picture = { R.drawable.secondviewcommon1, R.drawable.kong,
			R.drawable.kong , R.drawable.kong, R.drawable.kong,
			R.drawable.kong };
	public int[] common_mark = { R.drawable.mark3, R.drawable.mark2,
			R.drawable.mark3, R.drawable.mark4, R.drawable.kong,
			R.drawable.kong };
	public int[] common_mark_back = { R.drawable.mark_back3, R.drawable.mark_back2,
			R.drawable.mark_back3, R.drawable.mark_back4, R.drawable.kong,
			R.drawable.kong };
	
	public String[] common_titlestr = { "基本操作-信号源选择", "", "",
			"", "", "" };

	public int[] setting_picture = { R.drawable.secondviewsetting1, R.drawable.secondviewsetting2,
			R.drawable.secondviewsetting3,R.drawable.kong };
	public int[] setting_mark = { R.drawable.mark4, R.drawable.mark1,
			R.drawable.mark2, R.drawable.kong, R.drawable.kong };
	public int[] setting_mark_back = { R.drawable.mark_back4, R.drawable.mark_back1,
			R.drawable.mark_back2, R.drawable.kong, R.drawable.kong };
	
	public String[] setting_titlestr = { "设置-网络设置","设置-蓝牙设置", "设置-系统升级","" };

	public int[] movie_picture = { R.drawable.secondviewmovie1, R.drawable.kong,
			R.drawable.kong, R.drawable.kong, R.drawable.kong };
	public int[] movie_mark = { R.drawable.mark4, R.drawable.mark2,
			R.drawable.mark3, R.drawable.kong, R.drawable.kong };
	public int[] movie_mark_back = { R.drawable.mark_back4, R.drawable.mark_back2,
			R.drawable.mark_back3, R.drawable.kong, R.drawable.kong };
	public String[] movie_titlestr = { "影视-直通好莱坞", "", "", "", "" };

	public int[] AirSharing_picture = { R.drawable.secondviewsharing1, R.drawable.secondviewsharing2,
			R.drawable.secondviewsharing3, R.drawable.secondviewsharing4};
	public int[] AirSharing_mark = { R.drawable.mark2, R.drawable.mark5,
			R.drawable.mark3, R.drawable.mark1, R.drawable.kong };
	
	public int[] AirSharing_mark_back = { R.drawable.mark_back2, R.drawable.mark_back5,
			R.drawable.mark_back3, R.drawable.mark_back1, R.drawable.kong };
	
	
	public String[] AirSharing_titlestr = { "多屏互动-多屏云享", "多屏互动-天赐派","多屏互动-Miracast", "多屏互动-创维微信" };

	
	
	public int[] Play_picture={ R.drawable.secondviewplay1, R.drawable.secondviewplay2,
			R.drawable.secondviewplay3, R.drawable.kong, R.drawable.kong };
	public int[] Play_mark={R.drawable.mark4,R.drawable.mark3,R.drawable.mark3,R.drawable.kong,R.drawable.kong};
	public int[] Play_mark_back={R.drawable.mark_back4,R.drawable.mark_back3,R.drawable.mark_back3,R.drawable.kong,R.drawable.kong};
	
	public String[] Play_titlestr={"娱乐应用-游戏","娱乐应用-应用","娱乐应用-购物"};
	
	
	public int[] admin_picture = { R.drawable.secondviewadmin1, R.drawable.kong,
			R.drawable.kong, R.drawable.kong, R.drawable.kong,
			R.drawable.kong };
	public int[] admin_mark = { R.drawable.mark4, R.drawable.mark2,
			R.drawable.mark3, R.drawable.mark4, R.drawable.kong,
			R.drawable.kong };
	
	public int[] admin_mark_back = { R.drawable.mark_back4, R.drawable.mark_back2,
			R.drawable.mark_back3, R.drawable.mark_back4, R.drawable.kong,
			R.drawable.kong };
	
	
	public String[] admin_titlestr = { "账户注册", "", "",
			"", "", "" };
	
	
	public int[] media_picture = { R.drawable.secondviewmedia1, R.drawable.kong,
			R.drawable.kong, R.drawable.kong, R.drawable.kong,
			R.drawable.kong };
	public int[] media_mark = { R.drawable.mark4, R.drawable.mark2,
			R.drawable.mark3, R.drawable.mark4, R.drawable.kong,
			R.drawable.kong };
	
	public int[] media_mark_back = { R.drawable.mark_back4, R.drawable.mark_back2,
			R.drawable.mark_back3, R.drawable.mark_back4, R.drawable.kong,
			R.drawable.kong };
	
	
	public String[] media_titlestr = { "媒体播放", "", "",
			"", "", "" };
	
	
	public int[] question_picture = { R.drawable.secondviewquestion1, R.drawable.secondviewquestion2,
			R.drawable.kong, R.drawable.kong, R.drawable.kong };
	public int[] question_mark = {R.drawable.mark5, R.drawable.mark1,
			R.drawable.kong, R.drawable.kong, R.drawable.kong };
	
	public int[] question_mark_back = {R.drawable.mark_back5, R.drawable.mark_back1,
			R.drawable.kong, R.drawable.kong, R.drawable.kong };
	
	public String[] question_titlestr = { "疑难解答", "联系方式", "", "", "" };

	private String ReceiveStr;
	Intent intent;	
	FrameLayout mylayout;
	ImageView imageWindow, imageOne, imageTwo1, imageTwo2, imageThree1,
			imageThree2, imageThree3, imageThree4, imageFour1, imageFour2,
			imageFour3, imageFour4, imageFour5, imageFive;
	TextView marktext;


	
	
	private int commonView = 0, settingView = 1, sharingView = 2, playView = 3,
			adminView = 4, movieView = 5, mediaView = 6, questionView = 7;



	int[] pic_index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		intent = new Intent();
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  
		initSecondView();
		Intent intent = getIntent();
		ReceiveStr = intent.getStringExtra("type");

		if (ReceiveStr.equals("common")) {
			Log.i("lhy", "************************commonView");
			titletext.setText(common_titlestr[0]);
			ContentData.viewtype = commonView;
			picnum = 1;
			
			initshowdata(common_picture, common_mark,common_mark_back, common_titlestr);

		}
		if (ReceiveStr.equals("setting")) {
			Log.i("lhy", "***********************settingView");
			titletext.setText(setting_titlestr[0]);
			ContentData.viewtype = settingView;
			picnum = 3;
			initshowdata(setting_picture, setting_mark, setting_mark_back,setting_titlestr);

		}
		if (ReceiveStr.equals("AirSharing")) {
			Log.i("lhy", "*********************AirSharing");
			titletext.setText(AirSharing_titlestr[0]);
			ContentData.viewtype = sharingView;
		    /*************test********picnum = 4;**************/
			picnum=4;
			initshowdata(AirSharing_picture, AirSharing_mark,AirSharing_mark_back,
					AirSharing_titlestr);

		}
		if (ReceiveStr.equals("play")) {
			Log.i("lhy", "*******************playView");
			picnum = 3;
			ContentData.viewtype = playView;
			titletext.setText(Play_titlestr[0]);
			initshowdata(Play_picture,Play_mark,Play_mark_back,Play_titlestr);
		}
		if (ReceiveStr.equals("admin")) {
			Log.i("lhy", "******************adminView");
			titletext.setText(admin_titlestr[0]);
			ContentData.viewtype = adminView;
			initshowdata(admin_picture, admin_mark,admin_mark_back, admin_titlestr);
			picnum = 1;
		}

		if (ReceiveStr.equals("movie")) {
			Log.i("lhy", "*****************movie");
			titletext.setText(movie_titlestr[0]);
			ContentData.viewtype = movieView;
			picnum = 1;
			initshowdata(movie_picture, movie_mark,movie_mark_back, movie_titlestr);
		}
		if (ReceiveStr.equals("media")) {
			Log.i("lhy", "******************mediaView");
			ContentData.viewtype = mediaView;
			titletext.setText(media_titlestr[0]);
			initshowdata(media_picture, media_mark, media_mark_back,media_titlestr);
			picnum = 1;
		}
		if (ReceiveStr.equals("question")) {

			Log.i("lhy", "*****************questionView");
			titletext.setText(question_titlestr[0]);
			ContentData.viewtype = questionView;
			picnum = 2;
			initshowdata(question_picture, question_mark,question_mark_back, question_titlestr);

		}
		/**************************** lhy *************************/
		pic_index = new int[picnum];
		for (int i = 0; i < picnum; i++) {
			pic_index[i] = i;
		}
		
		
	    if(picnum==1){
	    	egOnly.setVisibility(View.VISIBLE);
	    	egCommon.setVisibility(View.INVISIBLE);
	    }else{
	    	egCommon.setVisibility(View.VISIBLE);
	    	egOnly.setVisibility(View.INVISIBLE);
	    }
		createPictureView(picnum);
		SecondViewPageNum = 1;
		initAnimation();

		System.gc();
	}

	private void initSecondView() {
		setContentView(R.layout.activity_second);
		
		egOnly = (TextView) findViewById(R.id.egonly);
		egCommon=(TextView)findViewById(R.id.egcommon);
		
		
		titletext = (TextView) findViewById(R.id.titletext);
		myabslayout = (AbsoluteLayout) findViewById(R.id.myabslayout);
		zeroimage = (ImageView) findViewById(R.id.zeroimage);
		enlargeimage = (ImageView) findViewById(R.id.enlargeimage);

		mSencondView = new ImageView[5];
		mSencondViewback=new ImageView[5];
		
		
		mSencondView[0] = (ImageView) findViewById(R.id.firstimage);
		mSencondView[1] = (ImageView) findViewById(R.id.secondimage);
		mSencondView[2] = (ImageView) findViewById(R.id.thirdimage);
		mSencondView[3] = (ImageView) findViewById(R.id.fourthimage);
		mSencondView[4] = (ImageView) findViewById(R.id.fifthimage);		
		
		secondotherimage = (ImageView) findViewById(R.id.secondotherimage);
		thirdotherimage = (ImageView) findViewById(R.id.thirdotherimage);
	
		
		
	}

	private void initAnimation() {

		myAnimationRight[0] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.righta);
		myAnimationRight[1] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.rightb);
		myAnimationRight[2] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.rightc);
		myAnimationRight[3] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.rightd);
		myAnimationRight[4] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.righte);

		myAnimationLeft[0] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.lefta);
		myAnimationLeft[1] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.leftb);
		myAnimationLeft[2] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.leftc);
		myAnimationLeft[3] = AnimationUtils.loadAnimation(SecondActivity.this,
				R.anim.leftd);
		
		zeroleft = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.zeroleft);
		animationsecondotherright = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.secondotherright);
		animationsecondotherleft = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.secondotherleft);
		animationthirdotherright = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.thirdotherright);
		animationthirdotherleft = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.thirdotherleft);
	}


	void initshowdata(int[] pic, int[] mar, int[] mar_back,String[] titletext) {

		Log.i("lhy", "this the number is :" + picnum);
        Log.i("lhy","SecondViewPageNum="+SecondViewPageNum);
		for (int i = 0; i <picnum; i++) {
			this.picture[i] = pic[i];
			this.mark[i] = mar[i];
			this.mark_back[i]=mar_back[i];
			this.titlestr[i] = titletext[i];
		}

	}



	private void createPictureView(int picture_number) {

		for (int i = 0; i < picture_number; i++) {

	
			mSencondView[i].setVisibility(View.VISIBLE);
			mSencondView[i].setBackgroundResource(picture[i]);
			if(i==0){
				mSencondView[i].setImageDrawable((getResources()
						.getDrawable(mark[i])));
			}else{
				mSencondView[i].setImageDrawable((getResources()
						.getDrawable(mark_back[i])));
			}
	
		}
		if (picture_number == 4) {
            Log.i("lhy","picture_number==4 createPictureView******");
			zeroimage.setVisibility(View.VISIBLE);
			zeroimage.setBackgroundResource(picture[3]);
			zeroimage.setImageDrawable((getResources().getDrawable(mark[3]))); 
		}
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		switch (keyCode) {
		case SkyworthKeyCode.SKY_KEY_RIGHT: 
		case SkyworthKeyCode.SKY_KEY_SHORT_SHUTTLE_RIGHT:
		{
			
				Log.i("lhy","^^^^^^^^^^^^^^^^^^^^^^^^^key right^^^^^^^^^^^^^^^");
                Log.i("lhy","before*********SecondViewPageNum="+SecondViewPageNum);
               
                
				if(picnum!=1){/*******************if secondviewnumber is 1 *********nothing to do*/
				SecondViewPageNum++;
				if (SecondViewPageNum > picnum) {
					SecondViewPageNum = 1;
				}
				order_picture(0);
				Log.i("lhy", "now ******SecondViewPageNum=" + SecondViewPageNum);
				Log.i("lhy", "now ******PageNum=" + picnum);
				
				for (int i = 0; i < picnum; i++) {
					Log.i("lhy", "before  order_hpicture   &&&&&  pic_index." + i + "=" + pic_index[i]);
				} 
				
				if(picnum==2){
					secondotherimage.startAnimation(animationsecondotherright);
				}else if(picnum==3){
					thirdotherimage.startAnimation(animationthirdotherright);
				}else{
					mSencondView[4].startAnimation(myAnimationRight[4]);
				}
				for (int i = 0; i < picnum; i++) {
				
					
					mSencondView[i].startAnimation(myAnimationRight[i]);
				}
			
				zeroimage.startAnimation(myAnimationRight[0]);
				
				enlargeimage.setVisibility(View.GONE);
				enlargeimage.setBackground(mSencondView[1].getBackground());
				enlargeimage.setVisibility(View.GONE);
				

				for (int i = 0; i < picnum; i++) {
					if (enlargeimage
							.getBackground()
							.getConstantState()
							.equals(getResources().getDrawable(picture[i])
									.getConstantState())) {
						titletext.setText(titlestr[i]);
					}
				}
				}else{
					
					
					Log.i("lhy","only ~~~~do nothing");
					//Toast.makeText(this, "当前只有一张图片", Toast.LENGTH_LONG).show();
				}
				System.gc();
				
		}
			return true;
		case SkyworthKeyCode.SKY_KEY_LEFT: 
		case SkyworthKeyCode.SKY_KEY_SHORT_SHUTTLE_LEFT:
		{
				
				if(picnum!=1){/*******************if secondviewnumber is 1 *********nothing to do*/
				SecondViewPageNum--;
				if (SecondViewPageNum <= 0) {
					SecondViewPageNum = picnum;
				}
				Log.i("lhy", "******key left*****");
				Log.i("lhy", "******SecondViewPageNum=" + SecondViewPageNum);
				Log.i("lhy", "******PageNum=" + picnum);
				order_picture(1);
				zeroimage.startAnimation(zeroleft);
				if(picnum==2){
					mSencondView[0].setVisibility(View.INVISIBLE);
					mSencondView[1].setVisibility(View.INVISIBLE);
					mSencondView[2].setVisibility(View.INVISIBLE);
					mSencondView[3].setVisibility(View.INVISIBLE);
					zeroimage.startAnimation(zeroleft);
					mSencondView[0].startAnimation(myAnimationLeft[0]);
					secondotherimage.startAnimation(animationsecondotherleft);
					mSencondView[1].startAnimation(animationsecondotherleft);
				}else if(picnum==3){
					zeroimage.startAnimation(zeroleft);
					thirdotherimage.startAnimation(animationthirdotherleft);
					mSencondView[0].startAnimation(myAnimationLeft[0]);
					mSencondView[1].startAnimation(myAnimationLeft[1]);
					mSencondView[2].startAnimation(animationthirdotherleft);
				}else if(picnum==4){
					/***************test***********************/
					for (int i = 0; i < picnum; i++) {
						mSencondView[i].startAnimation(myAnimationLeft[i]);
					}
				}else{
					for (int i = 0; i < picnum-1; i++) {
						mSencondView[i].startAnimation(myAnimationLeft[i]);}
				}
    			enlargeimage.setVisibility(View.GONE);
				enlargeimage.setBackground(mSencondView[picnum-1].getBackground());
				enlargeimage.setVisibility(View.GONE);
				
				for (int i = 0; i < picnum; i++) {
					if (enlargeimage
							.getBackground()
							.getConstantState()
							.equals(getResources().getDrawable(picture[i])
									.getConstantState())) {
						titletext.setText(titlestr[i]);
					}
				}
				}else{
					
					Log.i("lhy","only~~~~do nothing");
					//Toast.makeText(this, "当前只有一张图片", Toast.LENGTH_SHORT).show();
				}
				System.gc();
		}
			return true;
		case SkyworthKeyCode.SKY_KEY_CENTER: {
			flag = 3;
			Log.i("lhy", "the key enter ********");
            enter_thirdActivty();
            System.gc();
		}
			return true;
		case SkyworthKeyCode.SKY_KEY_UP: {

		}
			return true;
		case SkyworthKeyCode.SKY_KEY_DOWN: {

		}
			return true;

		case SkyworthKeyCode.SKY_KEY_BACK: {
			finish();
			flag = 2;

		}
			return true;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);

	}
	private void enter_thirdActivty(){
		String strname;
        strname = String.valueOf(SecondViewPageNum);
        Log.i("lhy","secondviewpagenum"+SecondViewPageNum);
        Log.i("lhy","this strname="+strname);
		intent.putExtra("id",strname);
        intent.setClass(SecondActivity.this, ContentActivity.class);
        SecondActivity.this.startActivity(intent);	
	}

	void order_picture(int type) {
		Log.i("lhy", "~~~~~~~~~~~~~~~~order");		

		if(picnum==2){
			mSencondView[0].setVisibility(View.INVISIBLE);
			mSencondView[1].setVisibility(View.INVISIBLE);
			mSencondView[2].setVisibility(View.INVISIBLE);
			mSencondView[3].setVisibility(View.INVISIBLE);
			secondotherimage.setImageDrawable((getResources().getDrawable(mark_back[pic_index[0]])));
			secondotherimage.setBackgroundResource(picture[pic_index[0]]);
		}else if(picnum==3){
			mSencondView[2].setVisibility(View.INVISIBLE);
			mSencondView[3].setVisibility(View.INVISIBLE);
			thirdotherimage.setImageDrawable((getResources().getDrawable(mark_back[pic_index[0]])));
			thirdotherimage.setBackgroundResource(picture[pic_index[0]]);
		}else if(picnum==4){
			mSencondView[picnum].setImageDrawable((getResources().getDrawable(mark_back[pic_index[0]])));
			mSencondView[picnum].setBackgroundResource(picture[pic_index[0]]);
		}
			zeroimage.setBackgroundResource(picture[pic_index[picnum-1]]);			
            zeroimage.setImageDrawable((getResources().getDrawable(mark[pic_index[picnum-1]])));        
		for (int i = 0; i < picnum; i++) {

			mSencondView[i].setBackgroundResource(picture[pic_index[i]]);
		    mSencondView[i].setImageDrawable((getResources().getDrawable(mark_back[pic_index[i]])));
			
		}	
		for (int i = 0; i < picnum; i++) {
			Log.i("lhy", "before   &&&&&  pic_index." + i + "=" + pic_index[i]);
		}
		if (type == 0) {
		    mSencondView[1].setImageDrawable((getResources().getDrawable(mark[pic_index[1]])));
			Log.i("lhy","order type=0");
			
			for (int i = 0; i < picnum; i++) {
				pic_index[i] = pic_index[i] + 1;
				if (pic_index[i] > picnum - 1) {
					pic_index[i] = 0;
				}
			}
		} else {
			mSencondView[picnum - 1].setImageDrawable((getResources().getDrawable(mark[pic_index[picnum - 1]])));
			Log.i("lhy","order type=1");
			for (int i = 0; i < picnum; i++) {
				pic_index[i] = pic_index[i] - 1;
				if (pic_index[i] < 0) {
					pic_index[i] = picnum - 1;
				}
			}
		}
		for (int i = 0; i < picnum; i++) {
			Log.i("lhy", "now   &&&&&  pic_index." + i + "=" + pic_index[i]);
		}
		
		
	}


}
